package com.mew.game.chess;

import com.mew.common.rest.game.ChessEnterRoomRequest;
import com.mew.common.rest.game.ChessEnterRoomResponse;
import com.mew.common.rest.sas.EnterGameSasRequest;
import com.zebra.common.event.EventBusAsyncCenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
public class ChessEnterExitRoomController {

    @Autowired
    private ChessEnterRoomService chessEnterExitRoomService;

    @PostMapping(value = "/chess/enter")
    @ResponseBody
    public ChessEnterRoomResponse enterRoom(@RequestBody ChessEnterRoomRequest request){
        ChessEnterRoomResponse chessEnterRoomResponse = null;
        try {
            log.info("Chess Enter: {}", request.toString());
            chessEnterRoomResponse = chessEnterExitRoomService.enterRoom(request);
            EventBusAsyncCenter.post(EnterGameSasRequest.builder().userId(request.getUid()).gameId(request.getGameId()).roomId(request.getRoomId()).build()); //TODO 进入游戏
            return chessEnterRoomResponse;
        }catch (Exception e){
            if (null != chessEnterRoomResponse){
                log.error("Chess Enter event error,  uid:"+request.getUid(), e);
                return chessEnterRoomResponse;
            }
            log.error("Chess Enter uid:"+request.getUid(), e);
            return ChessEnterRoomResponse.builder().errorState(500).build();
        }
    }


    @GetMapping(value = "/chess/exit/{uid}/{exittype}")
    @ResponseBody
    public Integer exitRoom(@PathVariable("uid") long uid, @PathVariable("exittype") int exittype){
        log.info("Chess ExitRoom u: {}  ", uid);
        try {
            chessEnterExitRoomService.exitGame(uid, exittype);
            return 1;
        }catch (Exception e){
            log.error("Chess ExitRoom u "+uid, e);
            return  0;
        }
    }



}
